<?php
/**
 * @Author: www.speakphp.com
 * @Date:   2016-06-13 11:46:15
 * @Last Modified by:   Awe
 * @Last Modified time: 2018-10-31 16:51:02
 * @备注 此文件是用户登录之后就允许操作的 不设计到权限
 */
class UserController extends CenterController {
    public function init() {
        parent::init();
    }
    public function baseAction(){
        if($this->getRequest()->isPost()){
            $this->doEditbase();
            exit;
        }
        $AdminUserModel = new AdminUserModel();
        $info = $AdminUserModel->getAdminUserByUserId( $this->userInfo['cid']);
        $res = array('info' => $info );
        $this->displayTemplate("admin/editbase" , $res );
    }
    public function makeSecretAction(){
        $stringClass = new StringClass();
        $appid = $stringClass->getRandom(16 , 5 );
        $appsecret = $stringClass->getRandom(100 , 5 );
        echo $appsecret ;
    }
    private function doEditbase(){
        $params = $this->getParams();
        $cname = isset($params['cname'])?trim($params['cname']):'';
        $appsecret = isset($params['appsecret'])?trim($params['appsecret']):'';
        $notice_url = isset($_POST['notice_url'])?trim($_POST['notice_url']):'';
        if( empty($cname) ){
            Common::EchoResult(-3 , "必须输入客户名称");
        }

        $stringClass = new StringClass();
        $AdminUserModel = new AdminUserModel();
        $info = $AdminUserModel->getAdminUserByUserId($this->userInfo['cid']);

        if( !empty($appsecret) ){
            $len = $stringClass->abslength($appsecret);
            if( $len != 100 ){
                Common::EchoResult(-3 , "秘钥长度必须是100个英文字符");
            }
        }
        if( !empty($notice_url) ){
            if( $info['notice_url'] !=  $notice_url ){
                $header = get_headers($notice_url);
                if(!preg_grep("/200/", $header)){
                    Common::EchoResult(-3 , "URL地址无法访问,请填写正确的URL地址");
                }
            }
            //Common::EchoResult(-3 , "请输入重复密码");
        }
        $request = array(
            'cname' => $cname ,
            'appsecret' => $appsecret,
            'notice_url' => $notice_url
        );
        if( empty($appsecret) ){
            unset($request['appsecret']);
        }
        $Business = Common::ImportBusiness("AdminUser" );
        $data = $Business->editBase( $request,$this->userInfo['cid'] );

        Common::EchoResult($data['code'] , $data['msg']);
    }
}